package com.dream.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 匠人码农
 * @date 2020/11/06 16:14
 * 概要：
 *     登录的Servlet程序
 */

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //类型转换
        HttpServletRequest request = (HttpServletRequest) req;

        //获取用户名
        String userName = request.getParameter("username");

        //获取用户密码
        String passWord = request.getParameter("password");

        //验证是否存在
        if ("admin".equals(userName) && "123456".equals(passWord)) {
            //存在的情况，登录成功页面
            //用户名存储到sessin
            req.getSession().setAttribute("username",userName);
            //打印成功信息
            //中文支持设定
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().println("登录成功");

        } else{
            //不存在的情况，返回登录页面
            req.getRequestDispatcher("/login.jsp").forward(req,resp);

        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
